package com.example.weather.city_manager

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import com.example.weather.MainActivity
import com.example.weather.R
import com.example.weather.data.City
import com.example.weather.data.CityDao
import com.example.weather.data.WeatherDatabase
import java.util.ArrayList

/**
 * 城市管理活动类，用于管理城市的添加、删除和展示
 * @author Y7993
 */
class CityManagerActivity : AppCompatActivity(), View.OnClickListener {
    var addIv: ImageView? = null
    var backIv: ImageView? = null
    var deleteIv: ImageView? = null
    var cityLv: ListView? = null

    // 列表数据源
    var mDatas: MutableList<String?> = ArrayList<String?>()
    var hotCitys: Array<String?> = arrayOf<String?>(
        "北京",
        "太原",
        "沈阳",
        "重庆",
        "天津",
        "南宁",
        "杭州",
        "武汉",
        "烟台",
        "西安"
    )

    // 城市接口
    private var cityDao: CityDao? = null

    // 适配器
    private var adapter: CityManagerAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_city_manager)
        // 初始化
        cityLv = findViewById<ListView>(R.id.city_lv)
        backIv = findViewById<ImageView>(R.id.city_iv_back) // 跳转为主页面
        deleteIv = findViewById<ImageView>(R.id.city_iv_delete) // 跳转为删除城市页面
        addIv = findViewById<ImageView>(R.id.city_iv_add) // 跳转回搜索城市页面

        // 得到数据库跟接口
        // 数据库
        val weatherDatabase = WeatherDatabase.getDatabase(this)
        cityDao = weatherDatabase.getCityDao()
        // 获取数据库的数据
        mDatas = getData()

        // 添加点击监听事件
        addIv!!.setOnClickListener(this)
        deleteIv!!.setOnClickListener(this)
        backIv!!.setOnClickListener(this)

        // 创建适配器
        adapter = CityManagerAdapter(this, mDatas)
        // 设立适配器
        cityLv!!.adapter = adapter
    }

    override fun onResume() { // 退出页面时的执行操作
        super.onResume() // 继承父类的方法
        // 先清空数据
        mDatas.clear()
        // 得到更新后的数据
        mDatas = getData()
        // 创建新的适配器
        adapter = CityManagerAdapter(this, mDatas)
        // 增加适配器
        cityLv!!.adapter = adapter
        // 提示适配器更新
        adapter!!.notifyDataSetChanged()
    }

    /**
     * 获取城市数据源
     * @return 返回城市名称列表
     */
    private fun getData(): MutableList<String?> {
        val cityList: MutableList<String?> = ArrayList<String?>()
        var cities = cityDao!!.findAll()
        cities?.let {
            if (it.isEmpty()) {
                for (tmp_name in hotCitys) {
                    cityDao!!.insetCity(City(tmp_name))
                    println("插入了城市$tmp_name\n")
                }
                cities = cityDao!!.findAll()
            }
        }
        if (cities != null) {
            for (c in cities) {
                c?.let { cityList.add(it.getName()) }
                println(c)
            }
        }
        return cityList
    }

    /**
     * 处理点击事件，根据不同按钮的点击，跳转到不同的界面
     * @param view 触发的视图
     */
    override fun onClick(view: View) {
        if (view.id == R.id.city_iv_add) { // 跳转添加界面
            val intent_add = Intent(this, SearchCityActivity::class.java)
            startActivity(intent_add)
            println("从城市管理界面跳转到搜索界面" + "\n")
        } else if (view.id == R.id.city_iv_back) {
            val intent_back = Intent(this, MainActivity::class.java)
            startActivity(intent_back)
            println("从城市管理界面返回主页面" + "\n")
        } else if (view.id == R.id.city_iv_delete) {
            val intent_delete = Intent(this, DeleteCityActivity::class.java)
            startActivity(intent_delete)
            println("从城市管理界面跳转到删除城市页面" + "\n")
        }
    }
}
